package com.cz.nongyetongv5.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.adapter.RecordHistoryAdapter;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.RecordHistory;
import com.cz.nongyetongv5.bean.User;
import com.cz.nongyetongv5.utils.CharsetUtil;
import com.cz.nongyetongv5.utils.ConfigUtil;
import com.cz.nongyetongv5.utils.GoHomeUtil;

/**
 * 监督检查统计
 *
 * @author Mr
 *
 */
public class TongJiXinXiActivity extends Activity implements OnClickListener {

	private ListView lvList = null;

	private ViewGroup btnBack = null;

	private List data = null;

	private List buhegedata = null;

	private RecordHistoryAdapter adpAdapter = null;

	private ViewGroup llBottom = null;

	private Button btnHeGe = null;

	private Button btnBuHeGe = null;

	private ConfigUtil configUtil = null;

	private User user = null;

	@Override
	protected void onDestroy() {

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().remove(this);

		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		configUtil = new ConfigUtil(getResources());

		InitApplication initApplication = (InitApplication) getApplicationContext();

		initApplication.getActivities().add(this);

		setContentView(R.layout.tongjixinxi);

		user = (User) getIntent().getSerializableExtra("user");

		llBottom = (ViewGroup) findViewById(R.id.llBottom);

		btnBuHeGe = (Button) llBottom.findViewById(R.id.btnLeft);
		btnBuHeGe.setBackgroundColor(Color.parseColor("#0A9D7E"));
		btnBuHeGe.setOnClickListener(this);

		btnHeGe = (Button) llBottom.findViewById(R.id.btnRight);
		btnHeGe.setOnClickListener(this);

		btnBack = (ViewGroup) findViewById(R.id.btnBack);
		btnBack.setOnClickListener(this);

		lvList = (ListView) findViewById(R.id.lvList);

		data = (List) getIntent().getSerializableExtra("data");

		buhegedata = (List) getIntent().getSerializableExtra("buhegedata");

		String title = getIntent().getStringExtra("title");

		TextView tvTitle = (TextView) findViewById(R.id.tvTitle);

		tvTitle.setText(title);

		if (buhegedata != null && buhegedata.size() > 0) {
			adpAdapter = new RecordHistoryAdapter(this, buhegedata);
			// 显示底部工具条
			llBottom.setVisibility(View.VISIBLE);

		} else {
			adpAdapter = new RecordHistoryAdapter(this, data);
			// 隐藏底部工具条
			llBottom.setVisibility(View.GONE);
		}

		lvList.setAdapter(adpAdapter);

		lvList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
									int position, long arg3) {

				RecordHistory recordHostory = (RecordHistory) adpAdapter
						.getItem(position);

				String id = recordHostory.getId();

				Intent intent = new Intent(TongJiXinXiActivity.this,
						RecordHistoryDetailActivity.class);

				intent.putExtra("user", user);

				// 产品检测
				if (CharsetUtil.isNotNull(recordHostory.getChanPin())) {

					String url = configUtil.getPreHttpUrl()
							+ getResources()
							.getString(
									R.string.recordhistory_detail_chanpinjiance)
							+ id + "/from/1/username/" + user.getUsername();

					intent.putExtra("url", url);
				} else {

					if (configUtil.duchaxuncha()) {

						// 督查巡查

						String url = configUtil.getPreHttpUrl()
								+ getResources()
								.getString(
										R.string.recordhistory_detail_duchaxuncha)
								+ id + "/from/1/username/" + user.getUsername();

						intent.putExtra("url", url);
					} else {

						// 监督检查

						String url = configUtil.getPreHttpUrl()
								+ getResources()
								.getString(
										R.string.recordhistory_detail_jiandujiancha)
								+ id + "/from/1/username/" + user.getUsername();

						intent.putExtra("url", url);

					}

				}

				startActivity(intent);

			}
		});

		loadRightTitle();

	}

	/**
	 * 加载去主页的按钮
	 */
	public void loadRightTitle() {

		ViewGroup btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		View view = GoHomeUtil.createPopView(getApplicationContext(),
				new String[] { "主页" });

		// 弹出更多的菜单
		final PopupWindow pop = new PopupWindow(view,
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);

		// 需要设置一下此参数，点击外边可消失
		pop.setBackgroundDrawable(new BitmapDrawable());

		// 设置点击窗口外边窗口消失
		pop.setOutsideTouchable(true);

		// 设置此参数获得焦点，否则无法点击
		pop.setFocusable(true);

		final Button btn01 = (Button) pop.getContentView().findViewWithTag(
				"btn0");

		OnClickListener menuOnClick = new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (v == btn01) {
					GoHomeUtil.goHome(getApplicationContext());
				}
			}
		};

		btn01.setOnClickListener(menuOnClick);

		btnMeanMore = (ViewGroup) findViewById(R.id.btnMeanMore);

		btnMeanMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (pop.isShowing()) {
					// 隐藏窗口，如果设置了点击窗口外小时即不需要此方式隐藏
					pop.dismiss();
				} else {
					// 显示窗口
					pop.showAsDropDown(v);
				}

			}
		});

	}

	@Override
	public void onClick(View v) {

		if (v == btnHeGe) {
			btnBuHeGe.setBackgroundResource(R.drawable.btn_back_pressed);
			btnHeGe.setBackgroundColor(Color.parseColor("#0A9D7E"));

			adpAdapter = new RecordHistoryAdapter(this, data);
			lvList.setAdapter(adpAdapter);
		}

		if (v == btnBuHeGe) {
			btnHeGe.setBackgroundResource(R.drawable.btn_back_pressed);
			btnBuHeGe.setBackgroundColor(Color.parseColor("#0A9D7E"));

			adpAdapter = new RecordHistoryAdapter(this, buhegedata);
			lvList.setAdapter(adpAdapter);

		}

		if (v == btnBack) {
			onBackPressed();
		}

	}

}
